import utils from './../../utils'

import React, { Component,  } from 'react'
import PropTypes from 'prop-types'
import Container from '../../components/Container'
import Header from '../../components/Header'

class HtmlTemplate extends Component{
  static contextTypes={
    router:PropTypes.object.isRequired,
  };
  constructor(props) {
    super(props);
    console.log(props)
    this.state = {
      ...this.buildTitleUrl(props.params.type),
      html:''
    };
  }
  buildTitleUrl(type){
    var title,url;
    switch(type){
      case 'grade_help':
        title='等级的升级规则';
        url  = utils.url.cmsUpgradeRule
        break;
      case "score_help":
        title='如何赚取积分';
        url  = utils.url.cmsGetInter
        break;
      case "make_help":
        title='使用帮助';
        url  = utils.url.cmsUseHelp
        break;
      case "partner_help":
        title = "经销商规则";
        url  = utils.url.cmsPartnerRule
        break;
      case "register_help":
        title="思埠新微商服务规则";
        url = utils.url.xwsServiceAgreement
        break;
      case "everyday":
        title="每日一报";
        url:null
        break;
    }
    return { title, url };
  }
  componentDidMount(){
    console.log('did')
    let {url} = this.state
    if(!url) { // 每日一报
      this.setState({
        html:`
          <a href="http://a.app.qq.com/o/simple.jsp?pkgname=com.sibu.android.microbusiness">
          <img src="https://img.sibumbg.cn/G1/M00/9C/2D/CixGgVlbKkOAXZmLAAMmt8oAoVk457.png" />
          </a>
        `
      })
      return 
    }
    utils.get(this.state.url)
    .then(res=>{
      if(res.errorCode===0&&res.result){
        this.setState({
          html:res.result.content
        })
      }
    })
  }
  render(){
    return(
      <Container>
        <Header 
          leftBtn={true}
          leftBtnTouch={()=>{
            this.context.router.history.goBack();
          }}
          title={this.state.title}
        />
        <div className="containers"  dangerouslySetInnerHTML={{__html:this.state.html}}></div>
      </Container>
    )
  }
}

export default HtmlTemplate


